// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 22/03/2011 12:30:03 p.m.
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   ImportadorDeCompras.java

package com.luxsoft.sw3.impap.services;

import com.luxsoft.siipap.compras.model.Compra2;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.sw3.services.ComprasManager;
import java.io.PrintStream;
import java.util.*;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.jdbc.core.JdbcTemplate;

// Referenced classes of package com.luxsoft.sw3.impap.services:
//            DataSourceUtils

public class ImportadorDeCompras
{

    public ImportadorDeCompras()
    {
    }

    public Compra2 importarCompra(Long folio)
    {
        JdbcTemplate template = new JdbcTemplate(DataSourceUtils.getInstance().getPapelDataSource());
        String sqlMaestro = "select * from SX_COMPRAS2 where SUCURSAL_ID=1 and FOLIO=?";
        List maestros = template.queryForList(sqlMaestro, new Object[] {
            folio
        });
        if(maestros.isEmpty())
            throw new RuntimeException((new StringBuilder("No existe la compra: ")).append(folio).toString());
        Map maestro = (Map)maestros.get(0);
        System.out.println((new StringBuilder("Compra: ")).append(maestro).toString());
        String sqlPartidas = "select COMPRADET_ID,CLAVE,COMENTARIO,COSTO,DEPURACION,DEPURADO,DESC1,DESC2,DESC3,DESC4,DESC5,DESC6,DESCRIPCION,DESCF,FACTOR,IMPORTE_BRUTO,IMPORTE_DESC,IMPORTE_NETO,PRECIO,SOLICITADO,SUC_NAME,UNIDAD,version,SUCURSAL_ID,PRODUCTO_ID,COMPRA_ID,FOLIO_ORIGEN,ADUANA,REGISTRO_ADUANA,RECIBIDO_GLOBAL from sx_compras2_det where COMPRA_ID=?";
        List partidas = template.queryForList(sqlPartidas, new Object[] {
            maestro.get("COMPRA_ID")
        });
        System.out.println((new StringBuilder("Partidas: ")).append(partidas.size()).toString());
        String INSERT = "INSERT INTO SX_COMPRAS2 (";
        String VALUES = "(";
        Iterator entryIter;
        for(entryIter = maestro.entrySet().iterator(); entryIter.hasNext();)
        {
            java.util.Map.Entry entry = (java.util.Map.Entry)entryIter.next();
            String campo = (String)entry.getKey();
            INSERT = (new StringBuilder(String.valueOf(INSERT))).append(campo).toString();
            VALUES = (new StringBuilder(String.valueOf(VALUES))).append("?").toString();
            if(entryIter.hasNext())
            {
                INSERT = (new StringBuilder(String.valueOf(INSERT))).append(",").toString();
                VALUES = (new StringBuilder(String.valueOf(VALUES))).append(",").toString();
            }
        }

        INSERT = (new StringBuilder(String.valueOf(INSERT))).append(")").toString();
        VALUES = (new StringBuilder(String.valueOf(VALUES))).append(")").toString();
        entryIter = maestro.entrySet().iterator();
        Object params[] = new Object[maestro.keySet().size()];
        int index = 0;
        while(entryIter.hasNext()) 
        {
            java.util.Map.Entry entry = (java.util.Map.Entry)entryIter.next();
            params[index++] = entry.getValue();
        }
        System.out.println(INSERT);
        System.out.println(VALUES);
        System.out.println(ArrayUtils.toString(((Object) (params))));
        ServiceLocator2.getJdbcTemplate().update((new StringBuilder(String.valueOf(INSERT))).append(" VALUES ").append(VALUES).toString(), params);
        for(Iterator iterator = partidas.iterator(); iterator.hasNext(); ServiceLocator2.getJdbcTemplate().update((new StringBuilder(String.valueOf(INSERT))).append(" VALUES ").append(VALUES).toString(), params))
        {
            Map row = (Map)iterator.next();
            INSERT = "INSERT INTO SX_COMPRAS2_DET (";
            VALUES = "(";
            for(entryIter = row.entrySet().iterator(); entryIter.hasNext();)
            {
                java.util.Map.Entry entry = (java.util.Map.Entry)entryIter.next();
                String campo = (String)entry.getKey();
                INSERT = (new StringBuilder(String.valueOf(INSERT))).append(campo).toString();
                VALUES = (new StringBuilder(String.valueOf(VALUES))).append("?").toString();
                if(entryIter.hasNext())
                {
                    INSERT = (new StringBuilder(String.valueOf(INSERT))).append(",").toString();
                    VALUES = (new StringBuilder(String.valueOf(VALUES))).append(",").toString();
                }
            }

            INSERT = (new StringBuilder(String.valueOf(INSERT))).append(")").toString();
            VALUES = (new StringBuilder(String.valueOf(VALUES))).append(")").toString();
            entryIter = row.entrySet().iterator();
            params = new Object[row.keySet().size()];
            index = 0;
            while(entryIter.hasNext()) 
            {
                java.util.Map.Entry entry = (java.util.Map.Entry)entryIter.next();
                params[index++] = entry.getValue();
            }
            System.out.println(INSERT);
            System.out.println(VALUES);
            System.out.println(ArrayUtils.toString(((Object) (params))));
        }

        try
        {
            DataSourceUtils.getInstance().close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        Compra2 res = ServiceLocator2.getComprasManager().buscarInicializada((String)maestro.get("COMPRA_ID"));
        return res;
    }

    public static void main(String args[])
    {
        (new ImportadorDeCompras()).importarCompra(Long.valueOf(86L));
    }
}
